﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Mulberry.Model.Ent;
using OMG.Fill.Mulberry.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Mulberry.Model.UI.Controllers
{
    public class MulAnnualProducStatistController : ApiController
    {
        MulAnnualProducStatistServices mulAnnualProducStatistServices = new MulAnnualProducStatistServices();
        ReportServices reportServices = new ReportServices();
        DictServices dict = new DictServices();

        //[ApiActionAuthorize("获取表3全年蚕桑生产情况统计表T_MulAnnualProducStatist分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<MulAnnualProducStatistEntity>> PostPageList(MulAnnualProducStatistQuery query)
        {
            return mulAnnualProducStatistServices.GetPageList(query);
        }
        //[ApiActionAuthorize("新增表3全年蚕桑生产情况统计表T_MulAnnualProducStatist指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(MulAnnualProducStatistEntity model) {
            return mulAnnualProducStatistServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改表3全年蚕桑生产情况统计表T_MulAnnualProducStatist指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(MulAnnInfoModel model)
        {
            if (model.Id == null)
            {
                model.FillInYear = dict.GetKey(model.FillInYear);
                model.ReportId = reportServices.GetReportId(model.ReportCode, model.AreaCode, model.FillInYear);
                model.Id = mulAnnualProducStatistServices.GetModelId(model.ReportId).model.Id;
            }
            return mulAnnualProducStatistServices.UpdateInfo(model);
        }
        /// <summary>
        /// 全年蚕桑生产情况统计
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResResult<List<MulAnnStatsModel>> PostStatsList(MulNowModel model)
        {
            return mulAnnualProducStatistServices.GetStatsList(model);
        }
        public ResResult<List<MulAnnStatsModel>> PostStatsTimeList(MulNowModel model)
        {
            return mulAnnualProducStatistServices.GetStatsByTimeList(model);
        }
        //[ApiActionAuthorize("删除表3全年蚕桑生产情况统计表T_MulAnnualProducStatist指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return mulAnnualProducStatistServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取表3全年蚕桑生产情况统计表T_MulAnnualProducStatist指定对象操作")]
        //[ApiActionException]
        public ResResult<MulAnnualProducStatistEntity> Get(string id)
        {
            return mulAnnualProducStatistServices.GetModel(id);
        }
        /// <summary>
        /// 填报时获取春夏秋数据   并计算增减
        /// </summary>
        /// <param name="id"></param>
        /// <param name="AreaCode"></param>
        /// <param name="FillInYear"></param>
        /// <returns></returns>
        public List<MulAnnualProducStatistModel> PostDataList(MulNowModel model) {
            return mulAnnualProducStatistServices.GetDataList(model.Id,model.AreaCode,model.FillInYear); 
        }
        public ResResult<List<MulAnnStatsModel>> PostStatsDetailList(MulNowModel model)
        {
            return mulAnnualProducStatistServices.GetDetailStats(model);
        }
        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportCode, string FillInYear, string AreaCode,string ReportId)
        {
            AreaServices areaServices = new AreaServices();
            string AreaName = areaServices.GetByCodeModel(AreaCode).model.Name;
            MulNowModel model = new MulNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.ReportCode = ReportCode;
            model.Id = ReportId;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear + AreaName + "全年蚕桑生产情况统计表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulAnnualProducStatistServices.Method(model).GetBuffer());
            curContext.Response.End();
        }

        public void GetStatsExcelData(string FillInYear, string AreaCode, string Status)
        {
            DictServices dict = new DictServices();
            MulNowModel model = new MulNowModel();
            model.FillInYear = FillInYear;
            model.AreaCode = AreaCode;
            model.Status = Status;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = FillInYear  + "全年蚕桑生产情况统计表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulAnnualProducStatistServices.MethodStats(model).GetBuffer());
            curContext.Response.End();
        }
    }
}